package com.skyhigh.web.webservice.controllers;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;

import com.skyhigh.web.webservice.domain.Cliente;
import com.skyhigh.web.webservice.services.ClienteService;

@Controller
public class ClientesController {

	@Autowired
	private ClienteService service;
	
	protected final Log logger = LogFactory.getLog(getClass());
	
	@RequestMapping(value="/buscarClientes.htm", method = RequestMethod.GET)
	public String desplegarFormularioBusqueda(ModelMap model){	
				
		int totalObjetos = service.obtenerCartidadObjetos(); 		
		
		model.addAttribute("totalObjetos", totalObjetos);					
		model.addAttribute("listaClientes", null);
		
		return "clienteBuscar";
	}
	
	@RequestMapping(value="/buscarClientes.htm", method = RequestMethod.POST)
	public String listarBusqueda(ModelMap model, 
			@ModelAttribute("idCliente") Integer idCliente, 
			@ModelAttribute("nombreCompleto") String nombreCompleto,
			@ModelAttribute("email") String email, 
			@ModelAttribute("cedula") String cedula,		
			BindingResult result, SessionStatus status){
		
		String query = "FROM Cliente WHERE idCliente is not Null ";			
		if(idCliente != 0)
			query = query + "AND idCliente = " + idCliente.toString();
		else
			query = query + "AND idCliente LIKE '%' ";
				

		int totalObjetos = service.obtenerCartidadObjetos(); 		
		
		model.addAttribute("totalObjetos", totalObjetos);		
		model.addAttribute("listaClientes", service.obtenerObjetosByConsulta(query));		
		
		return "clienteBuscar";
	}
	
	
	@RequestMapping(value="/editarCliente.htm", method = RequestMethod.GET)
	public String editarObjeto(ModelMap model, @ModelAttribute("idCliente") Integer idCliente){
		
		model.addAttribute("cliente", service.obtenerObjeto(idCliente));	
		
		return "clienteEditar";
	}
	
	
	@RequestMapping(value="/guardarClienteExistente.htm", method = RequestMethod.POST)
	public String guardarObjetoExistente(ModelMap model, 
			@ModelAttribute("idCliente") Integer idCliente, 
			@ModelAttribute("nombreCompleto") String nombreCompleto,
			@ModelAttribute("email") String email, 
			@ModelAttribute("cedula") String cedula,		
			BindingResult result, SessionStatus status){
		
		
		Cliente cliente = service.obtenerObjeto(idCliente);
		cliente.setNombreCompleto(nombreCompleto);
		cliente.setEmail(email);
		cliente.setCedula(cedula);
		service.actualizar(cliente);
		
		int totalObjetos = service.obtenerCartidadObjetos(); 			
		
		model.addAttribute("totalObjetos", totalObjetos);				
		model.addAttribute("listaClientes", null);	
		
		return "clienteBuscar";
	}
	
	@RequestMapping(value="/crearCliente.htm", method = RequestMethod.GET)
	public String desplegarFormularioCrearObjeto(ModelMap model){								
		
		return "clienteNuevo";
	}
	
	@RequestMapping(value="/guardarClienteNuevo.htm", method = RequestMethod.POST)
	public String guardarObjetoNuevo(ModelMap model, 			
			@ModelAttribute("nombreCompleto") String nombreCompleto,
			@ModelAttribute("email") String email, 
			@ModelAttribute("cedula") String cedula,		
			BindingResult result, SessionStatus status){
		
		Cliente cliente = new Cliente();
		cliente.setNombreCompleto(nombreCompleto);
		cliente.setEmail(email);
		cliente.setCedula(cedula);				
		service.agregar(cliente);
		
		int totalObjetos = service.obtenerCartidadObjetos(); 			
		
		model.addAttribute("totalObjetos", totalObjetos);		
		model.addAttribute("listaClientes", null);	
		
		return "clienteBuscar";
	}
	
	@RequestMapping(value="/eliminarCliente.htm", method = RequestMethod.GET)
	public String eliminarObjeto(ModelMap model, @ModelAttribute("idCliente") Integer idCliente){	
		
		service.eliminar(idCliente);
		
		int totalObjetos = service.obtenerCartidadObjetos(); 	
		
		model.addAttribute("totalObjetos", totalObjetos);			
		model.addAttribute("listaClientes", null);
		
		return "clienteBuscar";
	}
   
}
